<?php

class Todayspick_AdminController extends Ac_AdminController
{
    public function dashboardAction()
    {
    }

    public function menuAction()
    {
        $this->view->index = $this->_getParam('index');
    }

    public function indexAction() 
    {
        $this->listAction();
    }
    
    public function listAction() 
    {        
        if(!empty($_POST['save'])){
			$tp = Model_Todayspick::find(new MongoId($_POST['section']));
			$arr = array();
			for($i = 0; $i < 20; $i++){
				$arr[] = !empty($_POST['ls'.$i]) ? $_POST['ls'.$i] : "";
			}
			
			$tp->listing_id = array_values($arr);
			$tp->save();
		}
		$sections = Model_Todayspick::all()->count();
		
		//echo ($sections);exit();
		if($sections == 0){
			//add homepage and category in db
			//Model_Todayspick::addSection();
			$cats = Model_Category::getLevel1();
	        //save the homepage section
	        $tp = new Model_Todayspick();
	        $tp->name = "Homepage";
			$arr = array();
			for($i = 0; $i < 20; $i++){
				if($i == 0) {
					$arr[] = 'Replace this with ad id';
					continue;
				}
				$arr[] = '';
			}
			$tp->listing_id = array_values($arr);
			$tp->save();
	
	        foreach($cats as $k => $cat) {
	        	if(empty($cat->name_en) || $cat->status!=1){
					continue;	
				}
				$tp = new Model_Todayspick();
	            $tp->name = $cat->name_en;
	            $tp->category_id = $cat->_id;
				$tp->listing_id = array_values($arr);
	            $tp->save();
	        }
	        //$this->view->sections = Model_Todayspick::getSection();
		}       
        $this->view->sections = Model_Todayspick::getSection();
        
        $this->render('list');
    }
    
    public function getValueAction(){
		if ($this->_request->isPost() && $this->_request->isXmlHttpRequest()) {
            /*if(Model_Bookmark::addListing($_POST['data'])) {
                $res = 'ok';
                $this->_helper->json(array('res' => $res, 'value'=>$this->view->t('Saved')));
            }*/
            $tp = Model_Todayspick::one(array('_id' => new MongoId($_POST['data'])));
			$ls = array();
			
			if(!empty($tp->listing_id)){
				foreach($tp->listing_id as $tp_ls){
					$ls[] = $tp_ls;
				}
			}
			//print_r($tp);
            $this->_helper->json(array('tp' => $ls));
        }
        exit;
	}
	
	/*public function getListingAction($data){
		$tp = Model_Todayspick::find(new MongoId($data));
		$ls = array();
		
		if(!empty($tp->listing_is)){
			foreach($tp->listing_id as $tp_ls){
				$ls[] = $tp_ls;
			}
		}
		return $ls;
	}*/
	
	public function addSectionAction(){
		//add homepage and category in db
		//Model_Todayspick::addSection();
		$cats = Model_Category::getLevel1();
        //save the homepage section
        $tp = new Model_Todayspick();
        $tp->name = "Homepage";
		$arr = array();
		for($i = 0; $i < 20; $i++){
			$arr[] = '';
		}
		$tp->listing_id = array_values($arr);
		$tp->save();

        foreach($cats as $k => $cat) {
        	if(empty($cat->name_en) || $cat->status!=1){
				continue;	
			}
			$tp = new Model_Todayspick();
            $tp->name = $cat->name_en;
            $tp->category_id = $cat->_id;
			$tp->listing_id = array_values($arr);
            $tp->save();
        }
		
		//$this->listAction();
	}
    
    public function getSectionAction(){
		//get active categories in cat HP
		//$lvl1 = Model_Category::getLevel1();
		
		
	}
    
    public function addAction() 
    {
        if (!empty($_GET['id'])) {
            
        } 
    }
    
    public function editAction()
    {
        $name         = $_POST['name_en'];
        $message      = $_POST['message_en'];
        $tagline      = $_POST['tagline_en'];
        $companyname  = $_POST['companyname'];
        $username     = $_POST['username'];
        //$image        = $_POST['image'];
        //$logo         = $_POST['logo'];
        
        echo '<pre>';
        print_r($_FILES);
        //exit;
        $image = $logo = '';
        $uploads_dir = PUBLIC_PATH.'/images/testimonial';
        foreach ($_FILES as $key => $file) {
            if ($file["error"] == UPLOAD_ERR_OK) {
                $tmp_name = $file["tmp_name"];
                $img_name     = $file["name"];
                //var_dump($tmp_name, $uploads_dir.'/'.$name);
                move_uploaded_file($tmp_name, $uploads_dir.'/'.$img_name);
                if ($key == 'image') {
                    $image = '/images/testimonial/'.$img_name;
                } elseif ($key == 'logo') {
                    $logo = '/images/testimonial/'.$img_name;
                }
            }
        }      
        var_dump($image, $logo);
        //exit;
        $user = Model_User::one(array('un'=>$username));
        
        $obj          = new Model_Testimonial();
        $obj->name    = $name;
        $obj->message = $message;
        $obj->tagline = $tagline;
        $obj->user_id = $user->_id;
        $obj->company_name = $companyname;
        $obj->image   = $image;
        $obj->logo    = $logo;
        $obj->save();

        echo 'ok';exit;
    }
    
    public function deleteAction()
    {
        //$key    = $_GET['id'];
        Model_Testimonial::remove();
        //$this->_redirect($_GET['refer']);
    }    
}
